﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BanXeTang
{
    public class VienDan : Character
    {
        public float tocDo;
        public bool duocBan = false;
        public bool banTrung = false;
      
        public override VisibleGameEntity Clone()
        {
            VisibleGameEntity newObject = new VienDan();
            newObject._nsprite = this._nsprite;
            newObject._sprite = new MySprite[newObject._nsprite];
            for (int i = 0; i < newObject._nsprite; i++)
            {
                newObject._sprite[i] = new MySprite(this._sprite[i].texture2d, X, Y);
            }

            newObject.X = this.X;
            newObject.Y = this.Y;
            ((VienDan) newObject).tocDo = this.tocDo;

            return newObject;
        }
        public override void Update(GameTime gameTime)
        {
            if (duocBan)
            {
                switch (huong)
                {
                    case 0:
                        {
                            Y -= tocDo;
                            break;
                        }
                    case 1:
                        {
                            X += tocDo;
                            break;
                        }
                    case 2:
                        {
                            Y += tocDo;
                            break;
                        }
                    case 3:
                        {
                            X -= tocDo;
                            break;
                        }
                }
            }
            if (BienMat())
            {
                X = -20;
                Y = -20;
                duocBan = false;
                banTrung = false;
            }
                
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (duocBan)
                _sprite[huong].Draw(gameTime, spriteBatch, Color.White);
        }

        public bool BienMat()
        {
            if (banTrung)
                return true;
            else
            {
                if (X >= Game1.screenWidth - GlobalSettings._DeltaX)
                    return true;
                if (Y >= Game1.screenHeight - GlobalSettings._DeltaY)
                    return true;
                if (X < 0)
                    return true;
                if (Y < 0)
                    return true;
            }
            return false;
        }
        public override bool CheckCollisions(MySprite ms)
        {
            for (int i = 0; i < _nsprite; i++)
            {
                if (_sprite[i].CheckCollisions(ms))
                {
                    return true;
                }
            }
            return false;
        }
        public bool CheckCollisions(VienDan vd)
        {
           for (int i = 0; i < _nsprite; i++)
            {
                for (int j = 0;j < vd._nsprite ; j++ )
                    if (_sprite[i].CheckCollisions(vd._sprite[j]))
                    {
                        return true;
                    }
            }
            return false;
        }

    }
}
